<?php
/**
 * Author:xsc
 * Time:2023/8/31 19:26
 */

namespace App\Repository;

use App\Models\Special;

class SpecialRepo extends BaseRepo
{
    /**
     * @param $params
     * @return int
     */
    public function getCountByCondition($params): int
    {
        return $this->getQuery($params)->count();
    }

    /**
     * @param $params
     * @return bool
     */
    public function isExist($params): bool
    {

        return $this->getQuery($params)->exists();
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getQuery($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = Special::query();
        if (!empty($params['special_album_id'])) {
            $query->where("special_album_id", $params['special_album_id']);
        }
        return $query;
    }

    /**
     * 根据商户号查询创作者全部藏品
     * @param string $access_mid
     * @param int $page
     * @param int $page_size
     * @return array
     * Author:xsc
     * Time:2023/8/31 20:25
     */
    public function getListByAccessMid(string $access_mid, int $page = 1, int $page_size = 20)
    {
        $fields = [
                's.create_time',
            's.apply_status', 's.reason', 's.cast_status', 's.status',
            's.type','s.title','s.thumb','s.id as special_id','s.all_count',
            's.ca_cast_count as cast_count','s.base_price','s.sale_count','s.sell_time',
        ];
        $res = Special::query()
            ->from('special', 's')
            ->join('merchants_info as i', 'i.mer_id', '=', 's.merchants_id')
            ->where('i.access_mid', $access_mid)
            ->orderBy('s.id', 'asc')
            ->forPage($page, $page_size)
            ->get($fields)
            ->toArray();
        return $res;
    }

    /**
     * 根据商户号查询创作者全部藏品数量
     * @param $access_mid
     * @return int
     * Author:xsc
     * Time:2023/9/7 13:41
     */
    public function getCountByAccessMid($access_mid)
    {
        $res = Special::query()
            ->from('special', 's')
            ->join('merchants_info as i', 'i.mer_id', '=', 's.merchants_id')
            ->where('i.access_mid', $access_mid)
            ->count();
        return $res;
    }

    /**
     * 根据商户号和藏品id查询藏品详情
     * @param int $special_id
     * @param string $access_mid
     * @return array
     * Author:xsc
     * Time:2023/10/16 15:37
     */
    public function detail(int $special_id, string $access_mid = '')
    {
        $fields = [
            's.id as special_id', 's.title', 's.thumb', 's.base_price', 's.all_count', 's.create_time',
            's.apply_status', 's.reason', 's.cast_status', 's.status', 's.ca_cast_count as cast_count',
            'i.mer_id', 'i.access_mid', 'i.platform_id',
        ];

        $where = [
            ['s.id', '=', $special_id],
        ];
        if ($access_mid) {
            $where[] = ['i.access_mid', '=', $access_mid];
        }

        $res = Special::query()
            ->from('special', 's')
            ->join('merchants_info as i', 'i.mer_id', '=', 's.merchants_id')
            ->where($where)
            ->first($fields);
        return $res ? $res->toArray() : [];
    }

    public function getById($id)
    {
        return Special::query()->find($id);
    }

    /**
     * 新增/更新藏品
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/9/1 16:01
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return Special::query()->where('id', $id)->update($data);
        } else {
            return Special::query()->insertGetId($data);
        }
    }

    /**
     * 更新藏品销量
     * @param int $id
     * @param int $num
     * @return int
     * Author:xsc
     * Time:2023/9/6 16:33
     */
    public function updateSaleCount(int $id, int $num = 1)
    {
        return Special::query()->where('id', $id)->increment('sale_count', $num);
    }

    /**
     * 更新藏品通兑数量
     * @param int $id
     * @param int $num
     * @return int
     * Author:xsc
     * Time:2023/9/20 16:09
     */
    public function updateExchangeNum(int $id, int $num = 1)
    {
        return Special::query()->where('id', $id)->increment('exchange_num', $num);
    }

    /**
     * 获取藏品数量
     * @param array $params
     * @return int
     * Author:xsc
     * Time:2023/10/9 13:50
     */
    public function getSpecialCount(array $params = [])
    {
        $where = [
            ['g.type', '=', 1],
            ['g.channel', '=', 2],
            //['s.type', '=', 9],
            //['s.status', '=', 1],
            //['s.apply_status', '=', 2],
            ['s.cast_status', '=', 3],
        ];

        if (isset($params['member_id'])) {
            $where[] = ['g.member_id', '=', $params['member_id']];
        }
        if (isset($params['sell_status'])) {
            $where[] = ['g.sell_status', '=', $params['sell_status']];
        }
        if (isset($params['merchants_id'])) {
            $where[] = ['s.merchants_id', '=', $params['merchants_id']];
        }
        if (isset($params['title'])) {
            $where[] = ['s.title', 'like', '%' . $params['title'] . '%'];
        }

        // 转赠时间限制
        $transfer_time = ConfigRepo::getInstance()->getConfigValue('transfer_time');
        $time_limit = date('Y-m-d H:i:s', time() - $transfer_time * 60 * 60);
        $raw = "if(`think_g`.`stand_time` is not null,`think_g`.`stand_time` < '" . $time_limit . "',`think_g`.`create_time` < '" . $time_limit . "')";

        $res = Special::query()
            ->from('special', 's')
            ->join('merchants_info as i', 's.merchants_id', '=', 'i.mer_id')
            ->join('special_album as a', 's.special_album_id', '=', 'a.id')
            ->leftJoin('goods as g', 's.id', '=', 'g.special_id')
            ->where($where)
            ->whereRaw($raw)
            ->groupBy('s.id')
            ->get('s.id')
            ->toArray();
        return count($res);
    }

    /**
     * 获取藏品列表
     * @param array $params
     * @return array
     * Author:xsc
     * Time:2023/10/9 13:50
     */
    public function getSpecialList(array $params = [])
    {
        $fields = 'think_s.id as special_id,think_s.type as special_type,think_s.title,think_s.thumb,think_s.base_price,think_s.ca_cast_count,think_s.create_time,
        think_i.user_name,think_i.portrait,think_a.name as album_name,count(1) as count ';
        $where = [
            ['g.type', '=', 1],
            ['g.channel', '=', 2],
            //['s.type', '=', 9],
            //['s.status', '=', 1],
            //['s.apply_status', '=', 2],
            ['s.cast_status', '=', 3],
        ];
        $page = isset($params['page']) ? $params['page'] : 1;
        $page_size = isset($params['page_size']) ? $params['page_size'] : 20;
        if (isset($params['member_id'])) {
            $where[] = ['g.member_id', '=', $params['member_id']];
        }
        if (isset($params['sell_status'])) {
            $where[] = ['g.sell_status', '=', $params['sell_status']];
        }
        if (isset($params['merchants_id'])) {
            $where[] = ['s.merchants_id', '=', $params['merchants_id']];
        }
        if (isset($params['title'])) {
            $where[] = ['s.title', 'like', '%' . $params['title'] . '%'];
        }

        // 转赠时间限制
        $transfer_time = ConfigRepo::getInstance()->getConfigValue('transfer_time');
        $time_limit = date('Y-m-d H:i:s', time() - $transfer_time * 60 * 60);
        $raw = "if(`think_g`.`stand_time` is not null,`think_g`.`stand_time` < '" . $time_limit . "',`think_g`.`create_time` < '" . $time_limit . "')";

        $res = Special::query()
            ->selectRaw($fields)
            ->from('special', 's')
            ->join('merchants_info as i', 's.merchants_id', '=', 'i.mer_id')
            ->join('special_album as a', 's.special_album_id', '=', 'a.id')
            ->leftJoin('goods as g', 's.id', '=', 'g.special_id')
            ->where($where)
            ->whereRaw($raw)
            ->groupBy('s.id')
            ->orderBy('s.id', 'desc')
            ->forPage($page, $page_size)
            ->get()
            ->toArray();
        return $res;
    }


}
